package xyz.jangle.design.memento;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Caretaker {
	File file;
	private Memento memento = null;
	public Caretaker() {
		super();
		this.file = new File("saveObject.txt");
	}
	
	public Memento getMemento() {
		if(!file.exists())return null;
		try {
			ObjectInputStream inObject = new ObjectInputStream(new FileInputStream("saveObject.txt"));
			memento = (Memento) inObject.readObject();
			inObject.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return memento;
	}
	
	public void saveMemento(Memento memento) {
		try {
			ObjectOutputStream outObject =  new ObjectOutputStream(new FileOutputStream("saveObject.txt"));
			outObject.writeObject(memento);
			outObject.flush();
			outObject.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
